/**
 * Created by Administrator on 2017/1/6.
 */
/**
 * Created by Administrator on 2017/1/6.
 */

var pageSize = 5;//每页条数
var pageStart = 1;//当前页码
var pageCount = 10;//总页数
window.onload =function(){
	$(".pagination a").eq(1).css("background-color", "#eee");
	setInfo();
    $(".tv_icon").click(function () {
        excelExport();
    });
};
var setInfo= function () {
	var url="/huaqiao/tourequest/showall?pageStart=" + pageStart + "&pageSize=" + pageSize + "&time=" + new Date().getTime();
	 $.getJSON(url, function (mgs) {
	        if (mgs == null || mgs.length == 0) {
	            alert("找不到数据");
	            return;
	        }
	 
    var str = "";
    for (var i = 0; i < mgs.length; i++) {
    	if(mgs[i].authStatus==1){
    		mgs[i].authStatus="已批准";
    	}else if(mgs[i].authStatus==0){
    		mgs[i].authStatus="审批中";
    	}else if(mgs[i].authStatus==2){
            mgs[i].authStatus="不批准";
        }else{
            mgs[i].authStatus="已撤销";
    	}
    	var modiDate = new Date(mgs[i].startTime);
    	var nodiDate = new Date(mgs[i].endTime);
        str += "<tr>";
        str += "<td>" + mgs[i].id + "</td>" +
            "<td>" + mgs[i].majorname + "</td>" +
            "<td>" + mgs[i].classname + "</td>" +
            "<td>" + mgs[i].studentname + "</td>" +
            "<td>" + DateUtils.format(new Date(mgs[i].startTime), "yyyy-MM-dd HH:mm:ss") + "</td>" +
            "<td>" + DateUtils.format(new Date(mgs[i].endTime), "yyyy-MM-dd HH:mm:ss") + "</td>"+
            "<td>" + mgs[i].teachername + "</td>"+
            "<td>" + mgs[i].requestReason + "</td>"+
            "<td>" + mgs[i].authStatus + "</td>";
        
        
        str += "</tr>";
    }
	
    $("#outer_tbody").html(str);
    url = "/huaqiao/tourequest/getCount?time=" + new Date().getTime();
    $.get(url, function (res) {
        pageCount = Math.floor((res + pageSize - 1) / pageSize);
        $("#pageCount").html(pageCount);
        setTableBTN();
    });
   
	});
}

//将timestamp格式转化为 YYYY-MM-DD HH-mm-SS
function setFullDate(num){
	   num+="";
	   if(num.length<2){
		   num="0"+num;
	   }
	   return num;
}

//设置翻页区域按钮
var setTableBTN = function () {
  $(".pagination li").each(function (i) {
      if (i == 0) {//上一页按钮
          this.onclick = function () {
              if (pageStart > 1) {
                  pageStart--;

                  if (pageStart < parseInt($(".pagination a").eq(1).html())) {
                      $(".pagination a").eq(5).html(pageStart + 4);
                      $(".pagination a").eq(4).html(pageStart + 3);
                      $(".pagination a").eq(3).html(pageStart + 2);
                      $(".pagination a").eq(2).html(pageStart + 1);
                      $(".pagination a").eq(1).html(pageStart);
                  }
                  $(".pagination a").each(function (i) {
                      $(".pagination a").eq(i).css("background-color", "#fff");
                      if (pageStart == parseInt($(".pagination a").eq(i).html())) {
                          $(".pagination a").eq(i).css("background-color", "#eee");
                      }
                  });

                  setInfo();
              } else {
                  alert("当前是第一页");
              }
          };
      } else if (i == 6) {//下一页按钮
          this.onclick = function () {
              if (pageStart < pageCount) {
                  pageStart++;

                  if (pageStart > parseInt($(".pagination a").eq(5).html())) {
                      $(".pagination a").eq(5).html(pageStart);
                      $(".pagination a").eq(4).html(pageStart - 1);
                      $(".pagination a").eq(3).html(pageStart - 2);
                      $(".pagination a").eq(2).html(pageStart - 3);
                      $(".pagination a").eq(1).html(pageStart - 4);
                  }
                  $(".pagination a").each(function (i) {
                      $(".pagination a").eq(i).css("background-color", "#fff");
                      if (pageStart == parseInt($(".pagination a").eq(i).html())) {
                          $(".pagination a").eq(i).css("background-color", "#eee");
                      }
                  });
                  setInfo();
              } else {

                  alert("当前是最后一页");
              }
          };
      } else {//普通翻页按钮
          this.onclick = function () {
              var index = parseInt(this.innerText);
              if (index > pageCount) {
                  index = pageCount;
              }
              pageStart = index;
              $(".pagination a").each(function (i) {
                  $(".pagination a").eq(i).css("background-color", "#fff");

              });
              $(".pagination a").eq(index).css("background-color", "#eee");
              // pageStart = parseInt(this.);
              setInfo();
          }
      }
  });
};
//Excel导出
var excelExport = function () {
    url = "/huaqiao/tourequest/excelExport?" + new Date().getTime();
    var form = $('<form method="POST" action="' + url + '">');
    // for (var i = 0; i < para.length; i++) {
    //     form.append($('<input type="hidden" name="para" value="' + para[i] + '">'));
    // }
    form.appendTo('body').submit().remove();
};